
export {}

export type BusinessClass = {
  emit: (name: string, ...args: Array<any>) => void
  on: (name: string, callback: Function) => void
}

type ParamsKey = string | number | symbol

type List = {
  [key: ParamsKey]: Array<Function>
}

class Bus implements BusinessClass {
  list: List
  constructor() {
    this.list = {}
  }
  emit(name: string, ...args: Array<any>) {
    let fn: Array<Function> = this.list[name] || []
    if (fn.length === 0) return
    fn.forEach(f => f.apply(this, args))
  }
  on(name: string, callback: Function) {
    let fn: Array<Function> = this.list[name] || []
    fn.push(callback)
    this.list[name] = fn
  }
}

export const bus = new Bus()